package com.example.musicdemo;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.musicdemo.databinding.ItemMainViewBinding;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * @类 MusicDemo
 * @作者: 吴金龙
 * @创建时间: 2022/2/7
 * @描述:
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
    private static final String TAG = "RecyclerViewAdapter";
    ArrayList<Song> musicsList;



    public RecyclerViewAdapter(ArrayList<Song> musicsList) {
        this.musicsList = musicsList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemMainViewBinding itemMainViewBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.item_main_view,parent,false);
        return new MyViewHolder(itemMainViewBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {

        Song song = musicsList.get(position);


        holder.itemMainViewBinding.setSong(song);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(),PlayActivity.class);

                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList("musicsList",musicsList);
                bundle.putInt("position",position);
                intent.putExtras(bundle);
                Log.d(TAG, "onClick: "+position+1);

                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                view.getContext().startActivity(intent);

            }
        });
    }




    @Override
    public int getItemCount() {
        return musicsList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ItemMainViewBinding itemMainViewBinding;
       public MyViewHolder(ItemMainViewBinding itemMainViewBinding){

           super(itemMainViewBinding.getRoot());
           this.itemMainViewBinding = itemMainViewBinding;

       }

    }
}
